<template>
	<view class="edit_main">
		<view class="edit_card">
			<view class="edit_op">
				<view class="flex">
					<view class="op_title">公司</view>
					<input class="edit_input_tag" v-model="work_experience.company" type="text" value="" placeholder="填写公司, 发现同事" maxlength="25" ></input>
				</view>
				
			</view>
			<view class="edit_line"></view>
			<view class="edit_op">
				<view class="flex">
					<view class="op_title">职务</view>
					<input class="edit_input_tag" v-model="work_experience.job" type="text" value="" placeholder="请填写职务" maxlength="25"></input>
				</view>
			</view>
			<view class="edit_line"></view>
			<view class="edit_op">
				<view class="flex">
					<view class="long_title">起止时间</view>
					<view class="time_input_tag" >
						<view class="outside" @tap="showpicker1">
							<input class="edit_time_tag" v-model="start_time" type="text" :disabled="true" placeholder="入职时间"></input>
						</view>						
						<view>-</view>
						<view class="outside_a" @tap="showpicker2">
							<input class="edit_time_tag" v-model="end_time" type="text" :disabled="true" placeholder="离职时间(在职不填)" placeholder-style="text-align: center;"></input>
						</view>
					</view>
									
				</view>
			</view>			
		</view>
		
		<view class="" style="display: flex;  justify-content:center;" >
			<view v-if="index" class="school_btn" @tap="revise">确认修改</view>
			<view v-else class="school_btn" @tap="submit">提交</view>
			<view v-if="index" class="school_btn1" @tap="revise1">确认删除</view>
		</view>
		
		<w-picker
		    mode="yearMonth" 
		    startYear="100"
		    endYear="2030"
		    :defaultVal="defaultVal1" 
			:selectList="selectList1"
		    :current="true" 
		    @confirm="onConfirm" 
		    ref="picker" 
		    themeColor="#5771FF"
		></w-picker>
		<w-picker
		    mode="yearMonth" 
		    startYear="100"
		    endYear="2030"
		    :defaultVal="defaultVal1" 
			:selectList="selectList1"
		    :current="true" 
		    @confirm="onConfirm1" 
		    ref="picker1" 
		    themeColor="#5771FF"
		></w-picker>
		<w-picker
		    mode="yearMonth" 
		    startYear="100"
		    endYear="2030"
		    :defaultVal="defaultVal1" 
			:selectList="selectList1"
		    :current="true" 
		    @confirm="onConfirm2" 
		    ref="picker2" 
		    themeColor="#5771FF"
		></w-picker>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				work_experience:{
					company:'',//公司
					job:'',//职务
					time:''//起止时间
				},
				index:'',
				start_time:'',
				end_time:'',
				defaultVal1:[],
				selectList1:[],
			}
		},
		onLoad(option){
			this.index = option.index?option.index:'';//修改时传过来
			if(option.index){
				this.work_experience = uni.getStorageSync("work_experience")[option.index];
				if(this.work_experience.time){
					var arr=this.work_experience.time.split('-');
					let start_time = arr[0].replace('年','-');
					start_time = start_time.replace('月','');
					this.start_time = start_time.replace('至今','');
					
					let end_time = arr[1].replace('年','-');
					end_time = end_time.replace('月','');
					this.end_time = end_time.replace('至今','');
				}
			}
		},
		methods:{
			showpicker1(){
				this.$refs.picker1.show();
			},
			showpicker2(){
				this.$refs.picker2.show();
			},
			//日期选择点击后 重置表格
			onConfirm1(e){
				this.start_time = e.result;
			},
			onConfirm2(e){
				this.end_time = e.result;
			},
			submit(){
				if(!this.work_experience.company){
					this.$api.msg('请填写公司')
					return false;
				}
				if(!this.work_experience.job){
					this.$api.msg('请填写职务')
					return false;
				}
				if(!this.start_time){
					this.$api.msg('请选择入职时间')
					return false;
				}
				if(this.start_time){
					let start_time = this.start_time.replace('-','年')+'月';
					if(this.end_time){
						let end_time = this.end_time.replace('-','年')+'月';
						this.work_experience.time = start_time + '-' + end_time
					}else{
						this.work_experience.time = start_time + '-' + '至今'
					}
				}
				let work_experience = uni.getStorageSync("work_experience");
				if(work_experience==''){
					work_experience = [];
				}
				work_experience.push(this.work_experience);
				uni.setStorageSync("work_experience",work_experience);
				uni.navigateBack();
			},
			revise(){
				if(!this.work_experience.company){
					this.$api.msg('请填写公司')
					return false;
				}
				if(!this.work_experience.job){
					this.$api.msg('请填写职务')
					return false;
				}
				if(!this.start_time){
					this.$api.msg('请选择开始时间')
					return false;
				}
				if(this.start_time){
					let start_time = this.start_time.replace('-','年')+'月';
					if(this.end_time){
						let end_time = this.end_time.replace('-','年')+'月';
						this.work_experience.time = start_time + '-' + end_time
					}else{
						this.work_experience.time = start_time + '-' + '至今'
					}
				}
				let work_experience = uni.getStorageSync("work_experience");
				if(this.work_experience.company=='' || this.work_experience.job==''){
					this.work_experience = ''
				}
				work_experience[this.index] = this.work_experience;
				uni.setStorageSync("work_experience",work_experience);
				uni.navigateBack();
			},
			revise1(){
				let work_experience = uni.getStorageSync("work_experience");
				work_experience.splice(this.index, 1);
				uni.setStorageSync("work_experience",work_experience);
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	@import "./addcompany.scss"
</style>